package com.yy.bolg.service.impl;

import com.yy.bolg.dao.FeedbackDao;
import com.yy.bolg.entity.FeedbackBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 上报故障/预警Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-11-02
 */
@Service("feedbackService")
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public void add(FeedbackBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsCustomerCode())){
            throw new LackParameterException("客户编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackContent())){
            throw new LackParameterException("上报文字描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackImgFile())){
            throw new LackParameterException("上报图片描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackProvince()) || StringUtils.isEmpty(bean.getPsFeedbackCity()) || StringUtils.isEmpty(bean.getPsFeedbackRegion())){
            throw new LackParameterException("地区不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackAddress())){
            throw new LackParameterException("详细地址不能为空");
        }

        if(bean.getPsFeedbackLongitude() <= 0){
            throw new LackParameterException("经度不合法");
        }

        if(bean.getPsFeedbackLatitude() <= 0){
            throw new LackParameterException("纬度不合法");
        }

        if(bean.getPsFeedbackType() != 1 && bean.getPsFeedbackType() != 2){
            throw new LackParameterException("上报类型不合法");
        }

        if(bean.getPsFeedbackClassification() != 1 && bean.getPsFeedbackClassification() != 2){
            throw new LackParameterException("上报分类不合法");
        }

        try {
            feedbackDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public void addHik(FeedbackBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsCustomerCode())){
            throw new LackParameterException("客户编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackContent())){
            throw new LackParameterException("上报文字描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackImgFile())){
            throw new LackParameterException("上报图片描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackProvince()) || StringUtils.isEmpty(bean.getPsFeedbackCity()) || StringUtils.isEmpty(bean.getPsFeedbackRegion())){
            throw new LackParameterException("地区不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackAddress())){
            throw new LackParameterException("详细地址不能为空");
        }

        if(bean.getPsFeedbackLongitude() <= 0){
            throw new LackParameterException("经度不合法");
        }

        if(bean.getPsFeedbackLatitude() <= 0){
            throw new LackParameterException("纬度不合法");
        }

        if(bean.getPsFeedbackType() != 1 && bean.getPsFeedbackType() != 2){
            throw new LackParameterException("上报类型不合法");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackHikNewDate())){
            throw new LackParameterException("海康最新上报时间不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackHikDeviceType())){
            throw new LackParameterException("海康上报设备类型不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackHikDeviceSerial())){
            throw new LackParameterException("海康上报设备序列号不能为空");
        }

        if(bean.getPsFeedbackClassification() != 1 && bean.getPsFeedbackClassification() != 2){
            throw new LackParameterException("上报分类不合法");
        }

        try {
            feedbackDao.addHik(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public void del(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackCode())) {
            throw new LackParameterException("上报编号不能为空");
        }

        if (null == feedbackDao.findOneByCode(bean.getPsFeedbackCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            feedbackDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }

    @Override
    public void upState(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackCode())) {
            throw new LackParameterException("上报编号不能为空");
        }

        if (null == feedbackDao.findOneByCode(bean.getPsFeedbackCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(bean.getPsFeedbackState() == 2){
            if(StringUtils.isEmpty(bean.getPsFeedbackRemarks())){
                throw new LackParameterException("驳回原因不能为空");
            }
        }

        try {
            feedbackDao.upState(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public FeedbackBean findOneByCode(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackCode())) {
            throw new LackParameterException("上报编号不能为空");
        }

        try {
            return feedbackDao.findOneByCode(bean.getPsFeedbackCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public List<FeedbackBean> findAllByCondition(FeedbackBean bean) throws Exception {
        try {
            return feedbackDao.findAllByCondition(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByConditionToObserve(FeedbackBean bean) throws Exception {
        try {
            return feedbackDao.findAllByConditionToObserve(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByConditionToObserveNew(FeedbackBean bean) throws Exception {
        try {
            return feedbackDao.findAllByConditionToObserveNew(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByHikWaring() throws Exception {
        try {
            return feedbackDao.findAllByHikWaring();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByHikFault() throws Exception {
        try {
            return feedbackDao.findAllByHikFault();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByHikWeek() throws Exception {
        try {
            return feedbackDao.findAllByHikWeek();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllByCustomerWeek() throws Exception {
        try {
            return feedbackDao.findAllByCustomerWeek();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findAllBySerialDate(FeedbackBean bean) throws Exception {
        try {
            return feedbackDao.findAllBySerialDate(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<FeedbackBean> findUnread(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        try {
            return feedbackDao.findUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public void upUnread(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        try {
            feedbackDao.upUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public void upAppointUnread(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackCode())) {
            throw new LackParameterException("上报编号不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsCustomerCode())) {
            throw new LackParameterException("客户编号不能为空");
        }

        try {
            feedbackDao.upAppointUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public void upAppointUnreadBackend(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackCode())) {
            throw new LackParameterException("上报编号不能为空");
        }

        try {
            feedbackDao.upAppointUnreadBackend(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public List<FeedbackBean> findUnreadBackend(FeedbackBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsFeedbackType())) {
            throw new LackParameterException("上报类型不能为空");
        }

        try {
            return feedbackDao.findUnreadBackend(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }
}
